package com.tenton.controller;

import com.tenton.MyException.DataExist;
import com.tenton.MyException.DataNotExist;
import com.tenton.pojo.Supplier;
import com.tenton.service.SupplierService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@ApiIgnore
@Controller
@ApiOperation("管理员供应商管理")
@RequestMapping("/admin/flatform/supply")
public class ASupplierController {
    @Autowired
    SupplierService supplierService;

    /**
     * 跳转到增加页面
     *
     * @return
     */
    @ApiOperation("跳转到增加页面")
    @GetMapping("/toAdd")
    public String toAdd() {
        return "admin/supplier/addSupplier";

    }

    /**
     * 先判断数据库有没有，有就更新，没有就新增
     *
     * @param supply
     * @param session
     * @param request
     * @return
     */
    @ApiOperation("新增供应商信息")
    @PostMapping("/insert")
    public String insert(Supplier supply, HttpSession session, HttpServletRequest request) {
        try {
            supplierService.insert(supply,session);
            return "redirect:/admin/flatform/supply/getAll";
        } catch (DataExist dataExist) {
            request.setAttribute("msg", "该供应商信息已存在！");
            return "admin/supplier/addSupplier";
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "供应商信息为空！");
            return "admin/supplier/addSupplier";
        }
    }

    /**
     * 跳转到修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("跳转到修改页面")
    @GetMapping("/toUpdate")
    public String editSupply(int id, Model model) {
        supplierService.selectByPrimaryKey(id, model);
        return "admin/supplier/editSupplier";
    }

    /**
     * 删除供应商信息
     *
     * @param id
     * @return
     */
    @ApiOperation("通过Id删除供应商信息")
    @GetMapping("/delete")
    public String delete(int id) {
        supplierService.deleteByPrimaryKey(id);
        return "redirect:/admin/flatform/supply/getAll";
    }

    /**
     * 修改供应商信息
     *
     * @param supply
     * @param model
     * @param session
     * @param request
     * @return
     */
    @ApiOperation("修改供应商信息")
    @PostMapping("/update")
    public String update(Supplier supply, Model model, HttpSession session, HttpServletRequest request) {
        try {
            boolean flag = supplierService.update(supply, model,session);
            if (flag) {
                return "redirect:/admin/flatform/supply/getAll";
            }
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "供应商信息为空！");
            return "admin/supplier/editSupplier";
        }
        return null;
    }

    /**
     * 获取所有供应商信息
     *
     * @param model
     * @param pn
     * @return
     */
    @ApiOperation("获取所有供应商信息")
    @GetMapping("/getAll")
    public String getAll_sup(ModelMap model,
                             @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        supplierService.getAll(pn, model);
        return "admin/supplier/getSuppliersList";

    }

    /**
     * 查询供应商信息
     *
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("查询供应商信息")
    @PostMapping("/getSupply")
    public String getSupply(int id, Model model) {
        supplierService.selectByPrimaryKey(id, model);
        return "admin/supplier/getSuppliers";

    }

    /**
     * 供应商名称模糊查询
     *
     * @param supName
     * @param pn
     * @param model
     * @return
     */
    @ApiOperation("供应商名称模糊查询")
    @GetMapping("/getByParams")
    public String getByParams(@ApiParam("供应商公司名") @RequestParam(value = "supName", required = false) String supName,
                              @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn,
                              Model model) {
        supplierService.getByParams(supName, pn, model);
        return "admin/supplier/getSupplierByParams";

    }
}
